/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.example;

import com.sun.star.comp.helper.BootstrapException;
import com.sun.star.lang.XComponent;
import com.sun.star.uno.UnoRuntime;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Wenod
 */
public class InsertToText {



    private XComponent xCurrentComponent;

    public void Insert(String a) {
        //get the desktop
        com.sun.star.frame.XDesktop xDesktop = getDesktop();

        xCurrentComponent = xDesktop.getCurrentComponent();

        com.sun.star.text.XTextDocument xTextDocument = (com.sun.star.text.XTextDocument) UnoRuntime.queryInterface(com.sun.star.text.XTextDocument.class, xCurrentComponent);


        try {
            // create TextEmbeddedObject
            com.sun.star.lang.XMultiServiceFactory xDocMSF = (com.sun.star.lang.XMultiServiceFactory) UnoRuntime.queryInterface(com.sun.star.lang.XMultiServiceFactory.class, xTextDocument);
            com.sun.star.text.XTextContent xObj = (com.sun.star.text.XTextContent) UnoRuntime.queryInterface(com.sun.star.text.XTextContent.class, xDocMSF.createInstance("com.sun.star.text.TextEmbeddedObject"));

            // set class id for formula object to determine the type of object to be inserted - fomrula
            com.sun.star.beans.XPropertySet xPS = (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xObj);
            xPS.setPropertyValue("CLSID", "078B7ABA-54FC-457F-8551-6147e776a997");
            xPS.setPropertyValue("AnchorType", com.sun.star.text.TextContentAnchorType.AS_CHARACTER);

            // insert object in document
            com.sun.star.text.XTextCursor xCursor = xTextDocument.getText().createTextCursor();
            com.sun.star.text.XTextRange xRange = (com.sun.star.text.XTextRange) UnoRuntime.queryInterface(com.sun.star.text.XTextRange.class, xCursor);
            xTextDocument.getText().insertTextContent(xRange, xObj, false);

            // access objects model - formula
            com.sun.star.document.XEmbeddedObjectSupplier xEOS = (com.sun.star.document.XEmbeddedObjectSupplier) UnoRuntime.queryInterface(com.sun.star.document.XEmbeddedObjectSupplier.class, xObj);
            com.sun.star.lang.XComponent xModel = xEOS.getEmbeddedObject();

            //access to the formula properties
            com.sun.star.beans.XPropertySet xPSs = (com.sun.star.beans.XPropertySet) UnoRuntime.queryInterface(com.sun.star.beans.XPropertySet.class, xModel);
            //set the String command to the property String:formula of object formula
            xPSs.setPropertyValue("Formula", a);

        } catch (Exception e) {
            e.printStackTrace(System.err);
        }

        System.out.println("Done");



    }

    public static com.sun.star.frame.XDesktop getDesktop() {
        com.sun.star.frame.XDesktop xDesktop = null;
        com.sun.star.lang.XMultiComponentFactory xMCF = null;

        try {
            com.sun.star.uno.XComponentContext xContext = null;

            xContext = com.sun.star.comp.helper.Bootstrap.bootstrap();

            xMCF = xContext.getServiceManager();
            if (xMCF != null) {
                System.out.println("Connected to a running office ...");

                Object oDesktop = xMCF.createInstanceWithContext("com.sun.star.frame.Desktop", xContext);
                xDesktop = (com.sun.star.frame.XDesktop) UnoRuntime.queryInterface(com.sun.star.frame.XDesktop.class, oDesktop);
            } else {
                System.out.println("Can't create a desktop. No connection, no remote office servicemanager available!");
            }
        } catch (BootstrapException ex) {
            Logger.getLogger(NewJFrame.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception e) {
            e.printStackTrace(System.err);
            System.exit(1);
        }

        return xDesktop;

    }

}
